require([ "dojo/dom-style", "dojo/_base/fx", "dojo/_base/declare", "dijit/registry", "dojo/dom" ], function(domStyle,
        fx, declare, registry, dom) {
    var Masker = declare(null, {
        overlayNode : null,
        constructor : function(nodeId) {
            this.overlayNode = dom.byId(nodeId);
        },
        endLoading : function() {
            fx.fadeOut({
                node : this.overlayNode,
                onEnd : function(node) {
                    domStyle.set(node, 'display', 'none');
                }
            }).play();
        }
    });
    loading = new Masker('loadingOverlay');
});

require([ "dojo/_base/lang", "dijit/layout/BorderContainer" ], function(lang) {
    loading.endLoading();
});

require([ "dojo/ready", "dijit/registry", "dojox/data/JsonRestStore", "dijit/Tree", "dijit/tree/ForestStoreModel",
        "dojox/layout/ContentPane" ], function(ready, registry, JsonRestStore, Tree, ForestStoreModel, ContentPane) {
    myStore = new JsonRestStore({
        target : "tree/admin/",
        labelAttribute : "name"
    });
    myModel = new ForestStoreModel({
        store : myStore,
        deferItemLoadingUntilExpand : true,
        query : "root",
        rootLabel : 'Root',
        childrenAttrs : [ "children" ]
    });
    ready(function() {
        (new Tree({
            model : myModel,
            openOnClick : true,
            persist : false,
            showRoot : false,
            onClick : function(item) {
                var tabPanel = registry.byId('tabs');
                var tabItem = registry.byId(item.$ref) || new ContentPane({
                    id : item.$ref,
                    title : item.name,
                    href : item.url,
                    refreshOnShow : true,
                    closable : true,
                    onDownloadError : function(error) {
                        if (error.status == '999') {
                            window.location = 'login/admin';
                        } else {
                            return this.errorMessage;
                        }
                    }
                });
                if (tabPanel.getIndexOfChild(tabItem) == -1) {
                    tabPanel.addChild(tabItem);
                }
                tabPanel.selectChild(tabItem);
            }
        })).placeAt('rootlessTree').startup();
    });
});

require([ "dojo/parser", "dojo/ready", "dijit/layout/BorderContainer", "dijit/layout/TabContainer",
        "dijit/layout/ContentPane", "dijit/layout/AccordionContainer" ], function(parser, ready) {
    ready(function() {
        parser.parse();
    });
});